package com.magictower.primary.service;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.WindowManager;

import com.magictower.primary.db.dao.AddressDao;
import com.magictower.primary.utils.ToastUtils;

/**
 * 来电提醒的服务
 * @author kejunzhan
 * @since 2017/12/13
 */

public class AddressService extends Service {

    private WindowManager mWM;
    private View view;
    private TelephonyManager tm;
    private MyListener listener;
    private OutCallReceiver receiver;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mWM = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        //电话监听
        tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
        listener = new MyListener();
        tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);//监听电话状态

        receiver = new OutCallReceiver();
        IntentFilter filter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL);//监听去电广播
        registerReceiver(receiver,filter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        tm.listen(listener, PhoneStateListener.LISTEN_NONE);//停止监听
        unregisterReceiver(receiver);//注销广播
    }

    class MyListener extends PhoneStateListener {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state){
                case  TelephonyManager.CALL_STATE_RINGING:
                    //响铃状态，显示归属地
                    String address = AddressDao.getAddress(incomingNumber);
                    view = ToastUtils.showWindowToast(AddressService.this, mWM, address);
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    //闲置状态，关闭显示
                    if (mWM !=null && view !=null) {
                        mWM.removeView(view);
                        view = null;
                    }
                    break;
                case TelephonyManager.CALL_STATE_IDLE:
                    //闲置状态，关闭显示
                    if (mWM !=null && view !=null) {
                        mWM.removeView(view);
                        view = null;
                    }
                    break;
            }
            super.onCallStateChanged(state, incomingNumber);
        }
    }
    /**
     * 监听去电的广播接受者 需要权限: android.permission.PROCESS_OUTGOING_CALLS
     *
     * @author kejunzhan
     * @since 2017/12/12
     */
    class OutCallReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String phone = getResultData();//获取去电手机号
            String address = AddressDao.getAddress(phone);

            //Toast.makeText(context, address, Toast.LENGTH_SHORT).show();
            ToastUtils.showWindowToast(context, mWM, address);
        }
    }
}
